/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kylingroup.J2SE.IO;

/**
 *
 * @author kylin
 */
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;

public class SwingFileTransferProgress2 extends JFrame implements PropertyChangeListener{
    private static final long serialVersionUID = 1L;
    private static final int MIN = 0;
    private static final int MAX = 100;
    private JProgressBar progressBar;
    private JButton copy;
    private Task task;

    public SwingFileTransferProgress2(String title){
	setTitle(title);
	setLayout(new BorderLayout());
	progressBar = new JProgressBar(MIN, MAX);
	progressBar.setValue(MIN);
	progressBar.setStringPainted(true);
	add(progressBar, BorderLayout.CENTER);
	copy = new JButton("拷贝");
	copy.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
		start();
                copy.setEnabled(false);
            }
	});
	add(copy, BorderLayout.EAST);
    }

    class Task extends SwingWorker{
	@Override
	public Void doInBackground(){
            setProgress(MIN);
            File file = new File("/home/kylin/Shared/gradle-3.3-all.zip");
            long size = file.length();
            byte[] bts = new byte[(int) (size / (Math.random() * 50 + MAX - 50))];
            int length = 0;
            long cLength = 0;
            try{
		InputStream stream = new FileInputStream(file);
		OutputStream os = new FileOutputStream(new File("/home/kylin/Shared/2222.zip"));
		while((length = stream.read(bts)) > 0){
                    os.write(bts, 0, length);
                    cLength += length;
                    setProgress((int) (cLength * MAX / size));
                    Thread.sleep(30);
		}
                os.flush();
		os.close();
		stream.close();
            }
            catch(Exception e){
                e.printStackTrace();
            }
            return null;
        }

        @Override
	public void done(){
            setTitle("拷贝完毕");
            Toolkit.getDefaultToolkit().beep();
            copy.setEnabled(true);
            setCursor(null);
        }
    }

    public void propertyChange(PropertyChangeEvent evt){
	if("progress" == evt.getPropertyName()){
            int progress = (Integer) evt.getNewValue();
            progressBar.setValue(progress);
	}
    }

    private void start(){
	task = new Task();
	task.addPropertyChangeListener(this);
	task.execute();
	setTitle("正在拷贝...");
	setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    }

    private static void createAndShowGUI(){
	SwingFileTransferProgress2 frame = new SwingFileTransferProgress2("Bar");
	frame.setAlwaysOnTop(true);
	frame.setSize(280, 60);
	frame.setResizable(false);
	frame.setLocationRelativeTo(null);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setVisible(true);
    }

    public static void main(String[] args){
	javax.swing.SwingUtilities.invokeLater(new Runnable(){
            public void run(){
		createAndShowGUI();
            }
	});
    }
}